var key = getCookie('key');
var password,rcb_pay,pd_pay,payment_code;
// 现在支付方式
function toPay(order_amount,act,op) {
    $.ajax({
        type:'post',
        url:ApiUrl+'/index.php?act='+act+'&op='+op,
        data:{
            key:key,
            order_amount:order_amount,
        },
        dataType:'json',
        success: function(result){
            if (result.datas.error) {
                $.sDialog({
                    skin:"red",
                    content:result.datas.error,
                    okBtn:false,
                    cancelBtn:false
                });
                return false;
            }
            var pay_sn =  result.datas.pay_info.pay_sn;
            // 从下到上动态显示隐藏内容
            $.animationUp({valve:'',scroll:''});

            // 需要支付金额
            $('#onlineTotal').html(result.datas.pay_info.pay_amount);
            rcb_pay = 0;
            password = '';
            pd_pay = 0;
            payment_code = '';
            if (!$.isEmptyObject(result.datas.pay_info.payment_list)) {
                var readytoWXPay = false;
                var readytoAliPay = false;
                var m = navigator.userAgent.match(/MicroMessenger\/(\d+)\./);
                if (parseInt(m && m[1] || 0) >= 5) {
                    // 微信内浏览器
                    readytoWXPay = true;
                } else {
                    readytoAliPay = true;
                }
                for (var i=0; i<result.datas.pay_info.payment_list.length; i++) {
                    var _payment_code = result.datas.pay_info.payment_list[i].payment_code;
                    if (_payment_code == 'alipay' && readytoAliPay) {
                        $('#'+ _payment_code).parents('label').show();
                        if (payment_code == '') {
                            payment_code = _payment_code;
                            $('#'+_payment_code).attr('checked', true).parents('label').addClass('checked');
                        }
                    }
                    if (_payment_code == 'wxpay_jsapi' && readytoWXPay) {
                        $('#'+ _payment_code).parents('label').show();
                        if (payment_code == '') {
                            payment_code = _payment_code;
                            $('#'+_payment_code).attr('checked', true).parents('label').addClass('checked');
                        }
                    }
                }
            }

            $('#alipay').click(function(){
                payment_code = 'alipay';
            });

            $('#wxpay_jsapi').click(function(){
                payment_code = 'wxpay_jsapi';
            });

            $('#toPay').click(function(){
                if (payment_code == '') {
                    $.sDialog({
                        skin:"red",
                        content:'请选择支付方式',
                        okBtn:false,
                        cancelBtn:false
                    });
                    return false;
                }
                goToPayment(pay_sn,'pay_recharge');
            });
        }
    });
}

function goToPayment(pay_sn,op) {
    location.href = ApiUrl+'/index.php?act=member_payment&op='+op+'&key=' + key + '&pay_sn=' + pay_sn + '&password=' + password + '&rcb_pay=' + rcb_pay + '&pd_pay=' + pd_pay + '&payment_code=' + payment_code;
}
